package com.eportal.chinapay;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;




public class Tianjin {
	
	public static void main(String[] args) throws IOException{
		
		
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
//		String url = "http://131.252.83.233:9080/witsingletran/WitSinTranServlet";
		String url = "http://apps.chinapay.com/witsingletran/WitSinTranServlet";
		if(StringUtils.isEmpty(url)){
			
			url = "http://131.252.83.233:9080/witsingletran/WitSinTranServlet";
		}
		PostMethod postMethod = new PostMethod(url);
	    
		String merId = "808080450004842";
		String transDate = "20110628";
		String orderNo = "5000484200000012";
		String transType = "0003";
		String openBankId = "0303";
		String cardType = "0";
		String cardNo = "6226660601849124";
		
		String usrName1 = "����"; //�Ǳ���	
		String tianUn = toUnicode(usrName1);
//		String tianUn = usrName1;
//		System.out.println("tianUn=" + tianUn);
//		System.out.println("tianUn=" + tianUn);
		String tmpUsrName = tianUn;
//		System.out.println("tmpUsrName=" + tmpUsrName);
//		System.out.println("tmpUsrName=" + tmpUsrName);
		String certType = "01"; //�Ǳ���		
		String certId = "310115198312022735"; //�Ǳ���
		String curyId = "156";
		String transAmt = "000000000100";
		String purpose = "����";
		String priv1 ="00"; //�Ǳ���
		String tmpPriv1 = toUnicode(priv1);
//		System.out.println("uPriv1=" + tmpPriv1);
		String version = "20100831"; 
		String gateId = "7008";
		
		
		
//		System.out.println("gbk��:" + new String(usrName1.getBytes(),"GBK"));
//		System.out.println("gbk��:" + tozhCN(usrName1));
//		System.out.println("gbk��:" + tozhCN(tmpPriv1));
		StringBuffer retStr = new StringBuffer();
		
		retStr.append(merId).append(transDate).append(orderNo).append(transType).append(openBankId)
		.append(cardType).append(cardNo).append(tmpUsrName).append(certType).append(certId)
		.append(curyId).append(transAmt).append(tmpPriv1).append(version).append(gateId);
		
		SignData signData = new SignData();
		String data0 = new String(Base64.encode(retStr.toString().getBytes()));
		String chkValue = signData.signForCP(merId,data0);
		System.out.println(chkValue);
//		ResourceBundle chinapay_3des = ResourceBundle.getBundle("path");
//		String chkValue = SecureUtil.digitalSign(merId, new String(Base64.encode(retStr.toString().getBytes())), chinapay_3des.getString("keyPath")+"MerPrK.key");
		
		//��������?���ֵ
		NameValuePair[] data = { 
				new NameValuePair("merId", merId),
				
				new NameValuePair("transDate", transDate),
				new NameValuePair("orderNo", orderNo),
				new NameValuePair("transType", transType),
				new NameValuePair("openBankId", openBankId),
				new NameValuePair("cardType", cardType),
				new NameValuePair("cardNo", cardNo),

				new NameValuePair("usrName", tmpUsrName),
				new NameValuePair("certType", certType),
				new NameValuePair("certId", certId),
				new NameValuePair("curyId", curyId),
				new NameValuePair("transAmt", transAmt),
				new NameValuePair("purpose", purpose),
				new NameValuePair("priv1", tmpPriv1),
 				new NameValuePair("version", version),
				new NameValuePair("gateId", gateId),
				new NameValuePair("chkValue",chkValue)
		};
		
		// ���?��ֵ����postMethod��
		postMethod.setRequestBody(data);
		// ִ��postMethod
		int statusCode = 0;
		try {
			statusCode = httpClient.executeMethod(postMethod);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// HttpClient����Ҫ����ܺ�̷����������POST��PUT�Ȳ����Զ�����ת��
		if (statusCode != HttpStatus.SC_OK) {
			
			System.out.println("Method failed:" + postMethod.getStatusLine());
		}
		String tian = postMethod.getResponseBodyAsString();
		System.out.println("html:"+tian);
		
//		System.out.println(new String(html.toString().getBytes(),"GBK"));
		int dex = tian.lastIndexOf("=");
//		System.out.println(tian.substring(dex + 1));
//		System.out.println(tozhCN(tian.substring(0,dex+1)));
		
		String tiakong = tian.substring(0,dex+1);
		String msg = "";
		String tmp = "";
		String tmp2 = "";
		if(tiakong.indexOf("message")!=-1){
			msg = tiakong.substring(tiakong.indexOf("message")+8);
			tmp = tiakong.substring(0,tiakong.indexOf("message")+8);
			tmp2 = msg.substring(msg.indexOf("&", 1));
			msg = msg.substring(0,msg.indexOf("&", 1));

		}
		tmp = tmp+tozhCN(msg)+tmp2;
		System.out.println(tmp);
		String tian2 = new String(Base64.encode(tmp.getBytes()));
		System.out.println("tian2:"+tian2);
		boolean tiankogn = new SignData().verifyForCP("999999999999999", tian2,tian.substring(dex + 1));
		System.out.println("" + tiankogn);
//		System.out.println(html.toString()); 
		
	}

	public static String tozhCN(String unicode) {
//		System.out.println(unicode);
		StringBuffer gbk = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) { // ע��Ҫ�� 1 ��ʼ�����Ǵ�0��ʼ����һ���ǿա�
			int data = Integer.parseInt(hex[i], 16); // ��16������ת��Ϊ 10���Ƶ���ݡ�
			gbk.append((char) data); // ǿ��ת��Ϊchar���;������ǵ������ַ��ˡ�
		}
//		System.out.println("���Ǵ� Unicode���� ת��Ϊ �����ַ���: " + gbk.toString());
		return gbk.toString();
	}
 
 /**
     * ��String��ת��ΪUnicode����
     * 2010-10-20
     *
     */
    public static String toUnicode(String zhStr) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < zhStr.length(); i++) {
            char c = zhStr.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }
        System.out.println(unicode.toString());
        return unicode.toString();
    }
}
